package com.bdqn.bookshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bdqn.bookshop.common.ClassifyLevelEnum;
import com.bdqn.bookshop.dao.ClassifyMapper;
import com.bdqn.bookshop.entity.Classify;
import com.bdqn.bookshop.entity.Garousel;
import com.bdqn.bookshop.service.ClassifyService;
import com.bdqn.bookshop.utils.ClassifyNodeBuilder;
import com.bdqn.bookshop.utils.DataGridViewResult;
import com.bdqn.bookshop.utils.Result;
import com.bdqn.bookshop.vo.ClassifyVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author : 罗若然[LUO_RUO_RAN]
 * @date : 2020-07-24 16:00
 **/
@Service
@Transactional
public class ClassifyServiceImpl extends ServiceImpl<ClassifyMapper, Classify> implements ClassifyService {

    @Override
    public List<Classify> getClassifyParent() {

        QueryWrapper<Classify> queryWrapper = new QueryWrapper<Classify>();
        queryWrapper.eq("category_level", ClassifyLevelEnum.LEVEL_ONE.getLevel());
        return baseMapper.selectList(queryWrapper);
    }

    @Override
    public DataGridViewResult getClassify(ClassifyVo classifyVo) {
        IPage<Classify> page = new Page<Classify>(classifyVo.getPage(),classifyVo.getLimit());
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<Classify>();
        queryWrapper.eq(classifyVo.getParentId()!=null,"parent_id",classifyVo.getParentId());
        queryWrapper.like(StringUtils.isNotBlank(classifyVo.getClassifyName()),"classify_name",classifyVo.getClassifyName());
        queryWrapper.ge(classifyVo.getStartTime()!=null,"create_time",classifyVo.getStartTime());
        queryWrapper.le(classifyVo.getEndTime()!=null,"create_time",classifyVo.getEndTime());
        baseMapper.selectPage(page,queryWrapper);
        return new DataGridViewResult(page.getTotal(),page.getRecords());
    }

    @Override
    public Result addClassify(Classify classify) {
        classify.setCategoryLevel(ClassifyLevelEnum.LEVEL_TWO.getLevel());
        classify.setCreateTime(new Date());
        classify.setChangeTime(new Date());
        if(classify.getParentId()==null||classify.getParentId()==0){
            classify.setParentId(0);
            classify.setCategoryLevel(ClassifyLevelEnum.LEVEL_ONE.getLevel());
        }
        if(baseMapper.insert(classify)>0){
            return Result.createSuccess("添加成功");
        }
        return Result.createError("添加失败");
    }

    @Override
    public Result updateClassify(Classify classify) {
        classify.setCategoryLevel(ClassifyLevelEnum.LEVEL_TWO.getLevel());
        classify.setChangeTime(new Date());
        if(classify.getParentId()==null||classify.getParentId()==0){
            classify.setParentId(0);
            classify.setCategoryLevel(ClassifyLevelEnum.LEVEL_ONE.getLevel());
        }
        if(baseMapper.updateById(classify)>0){
            return Result.createSuccess("修改成功");
        }
        return Result.createError("修改失败");
    }

    @Override
    public Result deleteClassify(Integer id) {
        if(baseMapper.selectSublevelCountById(String.valueOf(id))>0){
          return Result.createError("该分类存在子级无法删除");
        }
        if(baseMapper.deleteById(id)>0){
            return Result.createSuccess("删除成功");
        }
        return Result.createError("删除失败");
    }

    @Override
    public Result batchDelete(String ids) {
        if(baseMapper.selectSublevelCountById(String.valueOf(ids))>0){
            return Result.createError("该分类存在子级无法删除");
        }
        String [] idsStr = ids.split(",");
        if (baseMapper.deleteBatchIds(Arrays.asList(idsStr))>0){
            return Result.createSuccess("删除成功");
        }
        return Result.createError("删除失败");
    }

    @Override
    public Result selectClassifyName(Integer parentId) {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<Classify>();
        queryWrapper.eq(parentId!=null,"id",parentId);
        Classify classify = baseMapper.selectOne(queryWrapper);
        if(classify!=null){
            return Result.createSuccess(classify);
        }
        return Result.createError();
    }

    @Override
    public Result isClassifyNameCount(Integer id, String classifyName) {
        QueryWrapper<Classify> queryWrapper = new QueryWrapper<Classify>();
        queryWrapper.eq(classifyName!=null,"classify_name",classifyName);
        Classify classify = baseMapper.selectOne(queryWrapper);
        if(id!=null){
            if (classify.getId()==id){
                return Result.createSuccess("可使用");
            }
        }
        if (classify!=null){
            return Result.createError("分类名存在重复");
        }
        return Result.createSuccess("可使用");
    }

    @Override
    public List<Classify> getClassifyNode() {

        List<Classify> classifies = baseMapper.selectList(null);

        List<Classify> classifyNodes = new ArrayList<Classify>();
        for (Classify classify : classifies) {
            classifyNodes.add(classify);
        }

       return ClassifyNodeBuilder.build(classifyNodes);
    }
}
